/*
 * Copyright (C) 2018 Pingan, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.batch.dashboard;

import com.pingan.haofang.searchcloud.common.constants.Constants;
import org.apache.commons.lang.time.DateUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * 监控基础类
 *
 * @author baojimin
 * @version 1.0
 * @date 2018/4/24 18:30
 */
public abstract class AbstractMonitorRunner {

    private static final String[] DATE_FORMATS = new String[]{Constants.DATE_FORMAT_YYYYMMDD};
    private static final int DEFAULT_DAYS_OF_RANGE = 7;

    /**
     * 校验参数，并parse为date类型
     *
     * @param args 程序参数
     * @return 返回时间数组，两个元素，第1个为开始时间，第2个为结束时间
     */
    public Date[] validAndParseDate(String[] args) throws ParseException {
        Date[] datePair = new Date[2];
        if (args.length == 2) {
            datePair[0] = DateUtils.parseDate(args[0], DATE_FORMATS);
            datePair[1] = DateUtils.parseDate(args[1], DATE_FORMATS);
        } else if (args.length == 0) {
            Date now = new Date();
            datePair[0] = DateUtils.truncate(DateUtils.addDays(now, -getDaysOfRange()), Calendar.DAY_OF_MONTH);
            datePair[1] = DateUtils.truncate(now, Calendar.DAY_OF_MONTH);
        } else {
            throw new IllegalArgumentException("date args number should be 0 or 2");
        }

        return datePair;
    }

    /**
     * 默认的日期区间
     *
     * @return
     */
    protected int getDaysOfRange() {
        return DEFAULT_DAYS_OF_RANGE;
    }


}
